package me.numark.view;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.library.bubbleview.BubbleLinearLayout;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.numark.R;
import me.numark.activities.BaseActivity;
import me.numark.activities.LabelsLikeActivity;
import me.numark.activities.MarkDetailActivity;
import me.numark.activities.ProfileActivity;
import me.numark.fragments.AskLoginFragment;
import me.numark.fragments.CommentsFragment;
import me.numark.model.FeedResponse;
import me.numark.model.FeedTag;
import me.numark.model.Message;
import me.numark.model.Topic;
import me.numark.model.User;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.Utils;
import me.numark.view.checktagview.TagLayoutView;
import me.numark.view.checktagview.TagListView;
import me.numark.view.tagview.OnTagClickListener;
import me.numark.view.tagview.Tag;
import me.numark.view.tagview.TagView;

/**
 * Created by Evan on 15/7/24.
 */
public class TopicViewHolder extends BaseViewHolder implements View.OnClickListener, OnTagClickListener, TagListView.OnTagCheckedChangedListener {
    @Bind(R.id.tag_list_item)
    @Nullable
    RelativeLayout tagListItem;
    @Bind(R.id.avatar)
    public ImageView avatar;

    @Bind(R.id.bubble_layout)
    LinearLayout bubbleLayout;

    @Bind(R.id.tagview)
    TagListView tagListView;

    @Bind(R.id.right_img)
    public TextView rightImage;



    public static final int STYLE_NONE = 0;
    public static final int STYLE_NORMAL = 1;
    Context mContext;
    long feedId;
    int type;
    public TopicViewHolder(View itemView, Object parent, int style,
                           MarkDetailActivity.OnCommentsFragmentInteractionListener clickTopicListener,long feedId,int type) {
        super(itemView, parent);
        if (style == STYLE_NONE)
            return;
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        addOnClickListener(avatar);
        addOnClickListener(rightImage);
        if (type==Constants.TOPICTYPE_LIST) {
            addOnClickListener(bubbleLayout);
        }

        this.feedId = feedId;
        this.type = type;
    }


    private FeedTag mFeedTag;


    public void bindData(FeedTag feedTag) {
        tagListView.fromType = 1;
        mFeedTag = feedTag;
        User user = mFeedTag.user;
        if (user != null)
            Picasso.with(mContext).load(feedTag.user.pic).into(avatar);
       /* Tag tag = new Tag(feedTag.content);
        tag.isDeletable=false;*/
       /* if (feedTag.self_like) {
            tag.layoutColor = Color.parseColor("#00b16a");
            tag.layoutBorderColor = Color.parseColor("#00b16a");
            tag.layoutBorderSize = 1;
            tag.tagTextColor = Color.parseColor("#ffffff");
        }else{
            tag.layoutColor = Color.parseColor("#ffffff");
            tag.layoutBorderColor = Color.parseColor("#999999");
            tag.layoutBorderSize = 1;
            tag.tagTextColor = Color.parseColor("#00b16a");
        }*/
       // tag.num = feedTag.like_num;
        tagListView.removeAllTags();

        if (type == Constants.TOPICTYPE_DETAIL){
            tagListView.fromType = 2;
        }

        tagListView.addTag(mFeedTag, true);
        tagListView.setOnTagCheckedChangedListener(this);



    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.avatar:
                ProfileActivity.openProfile(mContext, mFeedTag.user.uid);
                break;

            case R.id.right_image:
            case R.id.bubble_layout:
                //TODO 跳转至查看谁赞了页
                Intent intent = new Intent(mContext, LabelsLikeActivity.class);
                intent.putExtra(Constants.FEEDTAG_EXTRA,mFeedTag.toString());
                mContext.startActivity(intent);
                break;
        }
    }

    @Override
    public void onTagClick(Tag tag, int position) {
        if (!Global.isLoggedIn()) {
            AskLoginFragment.show((BaseActivity)mContext);
            return;
        }
        if (mFeedTag.user.uid == Global.getCurrentUid()) {
            if (itemView.getContext() instanceof MarkDetailActivity) {
                //提示删除标签
                ((MarkDetailActivity) itemView.getContext()).onClickComment(mFeedTag);
            }
        }else{
            //如果已经赞过 取消赞 同时更新详情页和列表页指定条目的UI
            new RequestServerTask<FeedResponse>(FeedResponse.class) {
                @Override
                protected String requestServer() {
                    String url = String.format(mFeedTag.self_like == true ? NetworkConstants.LABEL_HATE : NetworkConstants.LABEL_LIKE,
                            Global.getCurrentUid());
                    Map<String, Object> map = new HashMap();
                    map.put("label_id", mFeedTag.id);
                            /*if (mFeedType == Constants.FEEDTYPE_TAG_HOT) {
                                map.put("timeline_id", 0);
                            } else {
                                map.put("timeline_id", mFeed.id);
                            }*/
                    map.put("timeline_id",feedId);
                    return HttpUtil.post(url, map);
                }

                @Override
                protected void onSuccess(FeedResponse result) {
                    tagListView.removeAllTags();
                    Tag tag = new Tag(mFeedTag.content);
                    tag.isDeletable = false;

                    //如果是赞过的
                    if (mFeedTag.self_like){
                        //取消赞
                        mFeedTag.like_num--;
                        tag.num = mFeedTag.like_num;
                        tag.layoutColor = Color.parseColor("#ffffff");
                        tag.layoutBorderColor = Color.parseColor("#999999");
                        tag.layoutBorderSize = 1;
                        tag.tagTextColor = Color.parseColor("#00b16a");
                    }else{
                        mFeedTag.like_num++;
                        tag.num = mFeedTag.like_num;
                        tag.layoutColor = Color.parseColor("#00b16a");
                        tag.layoutBorderColor = Color.parseColor("#00b16a");
                        tag.layoutBorderSize = 1;
                        tag.tagTextColor = Color.parseColor("#ffffff");
                    }
                    //取反
                    mFeedTag.self_like = !mFeedTag.self_like;
                   // tagListView.addTag(tag);
                    ((MarkDetailActivity) itemView.getContext()).updateTimeLineLabelStatus(mFeedTag);
                }
            }.start();
        }
    }

    @Override
    public void onTagCheckedChanged(final TagLayoutView tagLayoutView,final FeedTag tag) {
        if (!Global.isLoggedIn()) {
            AskLoginFragment.show((BaseActivity) mContext);
            return;
        }
        if (mFeedTag.user.uid == Global.getCurrentUid()) {
            if (type == Constants.TOPICTYPE_DETAIL) {
                tagLayoutView.tagView.setChecked(mFeedTag.self_like);
            }else if (type == Constants.TOPICTYPE_LIST) {
                if (itemView.getContext() instanceof MarkDetailActivity) {
                    //提示删除标签
                    tagLayoutView.tagView.setChecked(mFeedTag.self_like);
                    if (type == Constants.TOPICTYPE_LIST) {
                        ((MarkDetailActivity) itemView.getContext()).onClickComment(mFeedTag);
                    }
                }
            }
        } else {
            //如果已经赞过 取消赞 同时更新详情页和列表页指定条目的UI
            //初始的状态
            if (type ==Constants.TOPICTYPE_DETAIL) {
                tagLayoutView.tagView.setChecked(mFeedTag.self_like);
            }else if (type == Constants.TOPICTYPE_LIST) {
                new RequestServerTask<FeedResponse>(FeedResponse.class) {
                    @Override
                    protected String requestServer() {
                        String url = String.format(mFeedTag.self_like == true ? NetworkConstants.LABEL_HATE : NetworkConstants.LABEL_LIKE,
                                Global.getCurrentUid());
                        Map<String, Object> map = new HashMap();
                        map.put("label_id", mFeedTag.id);
                            /*if (mFeedType == Constants.FEEDTYPE_TAG_HOT) {
                                map.put("timeline_id", 0);
                            } else {
                                map.put("timeline_id", mFeed.id);
                            }*/
                        map.put("timeline_id", feedId);
                        return HttpUtil.post(url, map);
                    }

                    @Override
                    protected boolean onFailure(FeedResponse result) {
                        tagLayoutView.tagView.setChecked(mFeedTag.self_like);
                        return super.onFailure(result);

                    }

                    @Override
                    protected void onSuccess(FeedResponse result) {
                        mFeedTag.self_like = !tag.self_like;
                        if (mFeedTag.self_like) {
                            mFeedTag.like_num++;
                        } else {
                            mFeedTag.like_num--;
                        }
                        tagLayoutView.textView.setText(String.valueOf(mFeedTag.like_num));
                       // tagLayoutView.tagView.setChecked(mFeedTag.self_like);
                        bindData(mFeedTag);
                        ((MarkDetailActivity) itemView.getContext()).updateTimeLineLabelStatus(mFeedTag);
                    }
                }.start();
            }
        }
    }
}



